<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
#
# Application template.
#
# This defines all the configurable parameters and other objects that are needed to run the jHipster application
# service. This template can even be saved in OpenShift namespace as well so that have the flexibility to do any project specific
# customizations. Pls note wherever displayName says *** PLEASE DO NOT CHANGE THIS ***, don't touch that as those parameters will be
# referenced in other places.
#
apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
kind: Template
metadata:
  name: <%= app.baseName.toLowerCase() %>-app-template
  namespace: <%= openshiftNamespace %>
  annotations:
    description: This template defines objects that are required to spin up an <%= app.baseName.toLowerCase() %> service pod
    tags: <%= app.baseName.toLowerCase() %>, service
    openshift.io/display-name: <%= app.baseName.toLowerCase() %>-template
    openshift.io/long-description: "This template provides objects that are required to spin up a <%= app.baseName.toLowerCase() %> service pod."
    openshift.io/provider-display-name: JHipster
labels:
  app: <%= app.baseName.toLowerCase() %>-app
  createdBy: JHipster-Team
parameters:
  -
    name: APPLICATION_NAME
    value: <%= app.baseName.toLowerCase() %>
    description: Name of the application
    required: true
    displayName: Application Name
  -
    name: SECRET_REF
    value: <%= app.baseName.toLowerCase() %>-ds-secret-ref
    description: Name of the DataSource Secret Reference
    required: true
    displayName: "*** PLEASE DO NOT CHANGE THIS ***"
  -
    name: REG_SECRET_REF
    value: reg-secret-ref
    description: Name of the Registry Secret Reference
    required: true
    displayName: "*** PLEASE DO NOT CHANGE THIS ***"
  -
    name: APP_PORT
    value: "<%= app.serverPort %>"
    description: Port of the application
    required: true
    displayName: Application Port
objects:
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: DeploymentConfig
    metadata:
      name: ${APPLICATION_NAME}
    spec:
      strategy:
        type: Rolling
        rollingParams:
          updatePeriodSeconds: 1
          intervalSeconds: 1
          timeoutSeconds: 600
          maxUnavailable: 25%
          maxSurge: 25%
          pre:
            failurePolicy: ignore
            execNewPod:
              command:
                - "/bin/sh"
                - "-c"
                - |
                    while true
                    do
                    <%_ if (app.prodDatabaseType === 'mysql') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-mysqldb 80)
                    <%_ } _%>
                    <%_ if (app.prodDatabaseType === 'mariadb') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-mariadb 80)
                    <%_ } _%>
                    <%_ if (app.prodDatabaseType === 'postgresql') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-postgresqldb 80)
                    <%_ } _%>
                    <%_ if (app.prodDatabaseType === 'mongodb') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-mongodb 80)
                    <%_ } _%>
                    <%_ if (app.prodDatabaseType === 'cassandra') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-cassandra 9042)
                    <%_ } _%>
                    <%_ if (app.prodDatabaseType === 'couchbase') { _%>
                      rt=$(nc -z -w 1 ${APPLICATION_NAME}-couchbase 8091)
                    <%_ } _%>
                      if [ $? -eq 0 ]; then
                        echo "DB is UP"
                        break
                      fi
                      echo "DB is not yet reachable;sleep for 10s before retry"
                      sleep 10
                    done
              containerName: ${APPLICATION_NAME}
        resources:
      replicas: 1
      template:
        metadata:
          labels:
            app: ${APPLICATION_NAME}
        spec:
          containers:
          - name: ${APPLICATION_NAME}
            image: <%= app.targetImageName %>
            imagePullPolicy: IfNotPresent
            env:
            - name: SPRING_PROFILES_ACTIVE
              value: prod,api-docs
            <%_ if (app.serviceDiscoveryType === 'eureka') { _%>
            - name: SPRING_CLOUD_CONFIG_URI
              value: http://admin:${jhipster.registry.password}@jhipster-registry:8761/config
            - name: EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE
              value: http://admin:${jhipster.registry.password}@jhipster-registry:8761/eureka/
            - name: JHIPSTER_REGISTRY_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${REG_SECRET_REF}
                  key: registry-admin-password
            <%_ } _%>
            <%_ if (app.serviceDiscoveryType === 'consul') { _%>
            - name: SPRING_CLOUD_CONSUL_HOST
              value: ${APPLICATION_NAME}-consul
            - name: SPRING_CLOUD_CONSUL_PORT
              value: 8500
            <%_ } _%>
            <%_ if (['mysql', 'mariadb', 'postgresql'].includes(app.prodDatabaseType)) { _%>
            - name: SPRING_DATASOURCE_URL
              <%_ if (app.prodDatabaseType === 'mysql') { _%>
              value: <%- getJDBCUrl(app.prodDatabaseType, { hostname: '${APPLICATION_NAME}'+`-${app.prodDatabaseType}db`, databaseName: app.baseName.toLowerCase() }) %>
              <%_ } else if (app.prodDatabaseType === 'mariadb') { _%>
              value: <%- getJDBCUrl(app.prodDatabaseType, { hostname: '${APPLICATION_NAME}'+`-${app.prodDatabaseType}`, databaseName: app.baseName.toLowerCase() }) %>
              <%_ } else if (app.prodDatabaseType === 'postgresql') { _%>
              value: <%- getJDBCUrl(app.prodDatabaseType, { hostname: '${APPLICATION_NAME}'+`-${app.prodDatabaseType}db`, databaseName: '${APPLICATION_NAME}' }) %>
              <%_ } _%>
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-user
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-password
            <%_ } _%>
            <%_ if (app.prodDatabaseType === 'mongodb') { _%>
            - name: SPRING_DATA_MONGODB_URI
              value: mongodb://${APPLICATION_NAME}-mongodb<% if (reactive) { %>/?waitQueueMultiple=1000<% } %>
            - name: SPRING_DATA_MONGODB_DATABASE
              value: ${APPLICATION_NAME}
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-user
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-password
            <%_ } _%>
            <%_ if (app.prodDatabaseType === 'cassandra') { _%>
            - name: SPRING_DATA_CASSANDRA_CONTACTPOINTS
              value: ${APPLICATION_NAME}-cassandra
            <%_ } _%>
            <%_ if (app.prodDatabaseType === 'couchbase') { _%>
            - name: SPRING_COUCHBASE_BOOTSTRAP_HOSTS
              value: ${APPLICATION_NAME}-couchbase
            - name: SPRING_COUCHBASE_BUCKET_NAME
              value: ${APPLICATION_NAME}
            <%_ } _%>
            <%_ if (app.searchEngine === 'elasticsearch') { _%>
            - name: SPRING_ELASTICSEARCH_REST_URIS
              value: http://${APPLICATION_NAME}-elasticsearch:9200
            <%_ } _%>
            <%_ if (app.messageBroker === 'kafka') { _%>
            - name: KAFKA_CONSUMER_KEY_DESERIALIZER
              value: 'org.apache.kafka.common.serialization.StringDeserializer'
            - name: KAFKA_CONSUMER_VALUE_DESERIALIZER
              value: 'org.apache.kafka.common.serialization.StringDeserializer'
            - name: KAFKA_CONSUMER_BOOTSTRAP_SERVERS
              value: jhipster-kafka:9092
            - name: KAFKA_CONSUMER_GROUP_ID
              value: '<%= dasherizedBaseName %>'
            - name: KAFKA_CONSUMER_AUTO_OFFSET_RESET
              value: 'earliest'
            - name: KAFKA_PRODUCER_BOOTSTRAP_SERVERS
              value: jhipster-kafka:9092
            - name: KAFKA_PRODUCER_KEY_DESERIALIZER
              value: 'org.apache.kafka.common.serialization.StringDeserializer'
            - name: KAFKA_PRODUCER_VALUE_DESERIALIZER
              value: 'org.apache.kafka.common.serialization.StringDeserializer'
            <%_ } _%>
            <%_ if (monitoring === 'prometheus') { _%>
            - name: JHIPSTER_METRICS_LOGS_ENABLED
              value: "true"
            - name: MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED
              value: "true"
            - name: JHIPSTER_METRICS_PROMETHEUS_ENDPOINT
              value: "/prometheusMetrics"
            <%_ } _%>
            ports:
            - name: http
              containerPort: ${{APP_PORT}}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Service
    metadata:
      annotations:
        service.alpha.openshift.io/dependencies: '[{
          "name": <%_ if (app.prodDatabaseType === 'mysql') { _%>"${APPLICATION_NAME}-mysqldb"<%_ } _%>
          <%_ if (app.prodDatabaseType === 'mariadb') { _%>"${APPLICATION_NAME}-mariadb"<%_ } _%>
          <%_ if (app.prodDatabaseType === 'postgresql') { _%>"${APPLICATION_NAME}-postgresqldb"<%_ } _%>
          <%_ if (app.prodDatabaseType === 'mongodb') { _%>"${APPLICATION_NAME}-mongodb"<%_ } _%>
          <%_ if (app.prodDatabaseType === 'cassandra') { _%>"${APPLICATION_NAME}-cassandra" <%_ } _%>, "namespace": "", "kind": "Service"}]'
      labels:
        app: ${APPLICATION_NAME}
      name: ${APPLICATION_NAME}
    spec:
      selector:
        app: ${APPLICATION_NAME}
      type: LoadBalancer
      ports:
      - name: http
        port: ${{APP_PORT}}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Route
    metadata:
      name: ${APPLICATION_NAME}
    spec:
      to:
        kind: Service
        name: ${APPLICATION_NAME}
        weight: 100
      port:
        targetPort: "http"
      wildcardPolicy: None
